package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"shop/api/request"
	"shop/common/global"
	"shop/common/pkg"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {

	var dev request.SendSmsReq
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	i := rand.Intn(9000) + 1000
	pkg.SendSms(dev.Account, strconv.Itoa(i))
	global.Rdb.Set(global.Ctx, fmt.Sprintf("sendSms%s", dev.Account), i, time.Minute)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "短信验证码发送成功",
		"data": i,
	})
}
